﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.IO;

namespace Moon.Sql
{
    public class SqlServerCodeFirst : ICodeFirst
    {
        private IDatabase Database;
        public SqlServerCodeFirst(IDatabase _Database)
        {
            Database = _Database;
        }
        public string Build<T>()
        {
            /*
            var tableInfo = Database.Query($"exec sp_helpdb [{Database.Connection.Database}]");
            string status = tableInfo.Rows[0]["status"].ToString();
            status = status.Substring(status.LastIndexOf("Collation=") + 10);
            string collate = status.Substring(0, status.IndexOf(", "));*/

            var table = ClassMapper.Mapping<T>();
            var tableName = table.TableName;
            StringBuilder builder = new StringBuilder();
            builder.Append($"create table [{tableName}](");
            foreach (var item in table.Properties)
            {
                if (item.Ignored) continue;
                //if(item.PropertyInfo.PropertyType == typeof(string) || item.PropertyInfo.PropertyType == typeof(char))
                //    builder.AppendLine($"{GetColumn(item)} COLLATE {collate},");
                //else
                    builder.AppendLine($"{GetColumn(item)},");
            }
            return builder.ToString().TrimEnd(",\r\n".ToArray()) + ")";
        }

        private string GetColumn(PropertyMap property)
        {
            string column = $"[{property.ColumnName}]";
            Type type = property.PropertyInfo.PropertyType;
            Dictionary<Type, string> hash = new Dictionary<Type, string>()
            {
                { typeof(bool), "bit"},
                { typeof(bool?), "bit"},
                { typeof(sbyte), "tinyint"},
                { typeof(sbyte?), "tinyint"},
                { typeof(short), "smallint"},
                { typeof(short?), "smallint"},
                { typeof(int), "int"},
                { typeof(int?), "int"},
                { typeof(long), "bigint"},
                { typeof(long?), "bigint"},
                { typeof(byte), "tinyint"},
                { typeof(byte?), "tinyint"},
                { typeof(ushort), "smallint"},
                { typeof(ushort?), "smallint"},
                { typeof(uint), "bigint"},
                { typeof(uint?), "bigint"},
                { typeof(ulong), "decimal"},
                { typeof(ulong?), "decimal"},
                { typeof(double), "double"},
                { typeof(double?), "double"},
                { typeof(float), "real"},
                { typeof(float?), "real"},
                { typeof(decimal), "decimal"},
                { typeof(decimal?), "decimal"},
                { typeof(DateTime), "datetime"},
                { typeof(DateTime?), "datetime"},
                { typeof(string), $"varchar({property.ColumnSize ?? 255})"},
                { typeof(char), "char(1)"},
            };
            column += " " + hash[type];
            if (property.IsIdentity)
            {
                column += $" identity";
            }
            if (property.IsPrimaryKey)
            {
                column += $" primary key";
            }
            return column;
        }
    }
}